package com.zwh.admin.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zwh.common.base.result.R;
import com.zwh.admin.entity.Permission;
import com.zwh.admin.service.PermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Api(tags = "管理员菜单接口")
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取全部菜单
     *
     * @return
     */
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public R indexAllPermission() {
        List<Permission> list = permissionService.queryAllMenuGuli();
        return R.ok().data("children", list);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R remove(@PathVariable String id) {
        permissionService.removeChildByIdGuli(id);
        return R.ok();
    }

    /**
     * 给角色分配权限
     * 往角色-权限(菜单)关联表(acl_role_permission)中添加数据
     *
     * @param roleId
     * @param permissionId
     * @return
     */
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    @Transactional(rollbackFor = Exception.class)
    public R doAssign(String roleId, String[] permissionId) {
        permissionService.saveRolePermissionRealtionShipGuli(roleId, permissionId);
        return R.ok();
    }

    /**
     * 根据角色获取菜单
     * 需要构建层次结构的菜单返回
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public R toAssign(@PathVariable String roleId) {
        List<Permission> list = permissionService.selectAllMenu(roleId);
        return R.ok().data("children", list);
    }


    /**
     * 添加菜单
     *
     * @param permission
     * @return
     */
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return R.ok();
    }

    /**
     * 修改菜单
     *
     * @param permission
     * @return
     */
    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    @Transactional(rollbackFor = Exception.class)
    public R updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return R.ok();
    }

}

